package com.fw.ui.view.common;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.fw.service.passwordpolicy.bean.PasswordPolicyBean;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PasswordPolicyBAO;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.AbstractPanel;
import com.fw.ui.view.common.event.ChangePasswordPanelSaveButtonClickListener;
import com.vaadin.server.Page;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.VerticalLayout;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
public class ChangePasswordPanel extends AbstractPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PasswordPolicyBAO passwordPolicyBAO;

    @Autowired
    private ChangePasswordPanelSaveButtonClickListener changePasswordPanelSaveButtonClickListener;

    private PasswordPolicyBean passwordPolicyBean;

    private Label lblTitle = new Label("", ContentMode.HTML);
    private Label lblOldPassword = new Label("", ContentMode.HTML);
    private Label lblNewPassword = new Label("", ContentMode.HTML);
    private Label lblConfirmNewPassword = new Label("", ContentMode.HTML);
    private Label lblPasswordHint1 = new Label("", ContentMode.HTML);
    private Label lblPasswordHint2 = new Label("", ContentMode.HTML);

    private Label lblOldPasswordValidateMessage = new Label("", ContentMode.HTML);
    private Label lblNewPasswordValidateMessage = new Label("", ContentMode.HTML);
    private Label lblConfirmNewPasswordValidateMessage = new Label("", ContentMode.HTML);

    private Label lblFooter = new Label("", ContentMode.HTML);

    private PasswordField txtOldPassword = new PasswordField();
    private PasswordField txtNewPassword = new PasswordField();
    private PasswordField txtConfirmNewPassword = new PasswordField();

    private Button btnSave = new Button();
    private Button btnCancel = new Button();

    private HorizontalLayout headerlayout = new HorizontalLayout();
    private VerticalLayout changePasswordLayout = new VerticalLayout();
    private VerticalLayout mainlayout = new VerticalLayout();

    private HorizontalLayout sepFooterlayout1 = new HorizontalLayout();
    private HorizontalLayout sepFooterlayout2 = new HorizontalLayout();

    private HorizontalLayout row1Validate = new HorizontalLayout();
    private HorizontalLayout row2Validate = new HorizontalLayout();
    private HorizontalLayout row5Validate = new HorizontalLayout();

    private Long userId;
    private String oldPassword;
    private String navigatePath;
    private boolean requireNewLogin;

    @Override
    public void updateStrings() {
	lblTitle.setValue(this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.label.title"));
	lblFooter.setValue(this.messageBAO.getMessage("application.title.footer"));

	lblOldPassword.setValue(this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.label.oldpassword"));
	lblNewPassword.setValue(this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.label.newpassword"));
	lblConfirmNewPassword.setValue(this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.label.confirmnewpassword"));

	btnSave.setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.button.save"));
	btnCancel.setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.button.cancel"));

	txtOldPassword.setInputPrompt(this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.inputprompt.password"));
	txtNewPassword.setInputPrompt(this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.inputprompt.password"));
	txtConfirmNewPassword.setInputPrompt(this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.inputprompt.password"));
    }

    @Override
    public void init() {
	this.setSizeFull();

	lblTitle.addStyleName("h2");
	lblFooter.setSizeUndefined();
	lblFooter.addStyleName("footer-layout-label");

	lblOldPassword.setWidth("180px");
	lblNewPassword.setWidth("180px");
	lblConfirmNewPassword.setWidth("180px");

	this.lblOldPasswordValidateMessage.setId("lblOldPasswordValidateMessage" + UICommonUtils.INCLUDE_VALUE_COMPONENT_PREFIX);
	this.lblNewPasswordValidateMessage.setId("lblNewPasswordValidateMessage" + UICommonUtils.INCLUDE_VALUE_COMPONENT_PREFIX);
	this.lblConfirmNewPasswordValidateMessage.setId("lblConfirmNewPasswordValidateMessage"
		+ UICommonUtils.INCLUDE_VALUE_COMPONENT_PREFIX);

	this.row1Validate.setId("row1Validate" + UICommonUtils.INCLUDE_VISIBLE_COMPONENT_PREFIX);
	this.row2Validate.setId("row2Validate" + UICommonUtils.INCLUDE_VISIBLE_COMPONENT_PREFIX);
	this.row5Validate.setId("row5Validate" + UICommonUtils.INCLUDE_VISIBLE_COMPONENT_PREFIX);

	this.lblOldPasswordValidateMessage.addStyleName("validate-error");
	this.lblNewPasswordValidateMessage.addStyleName("validate-error");
	this.lblConfirmNewPasswordValidateMessage.addStyleName("validate-error");

	Label lblSep1 = new Label();
	Label lblSep2 = new Label();
	Label lblSepValidate1 = new Label("");
	Label lblSepValidate2 = new Label("");
	Label lblSepValidate5 = new Label("");
	lblSep1.setWidth("180px");
	lblSep2.setWidth("180px");
	lblSepValidate1.setWidth("180px");
	lblSepValidate2.setWidth("180px");
	lblSepValidate5.setWidth("180px");

	lblPasswordHint1.addStyleName("label-lightblue");
	lblPasswordHint1.setWidth("450px");
	lblPasswordHint2.addStyleName("label-red");
	lblPasswordHint2.setWidth("450px");

	this.lblOldPasswordValidateMessage.setWidth("450px");
	this.lblNewPasswordValidateMessage.setWidth("450px");
	this.lblConfirmNewPasswordValidateMessage.setWidth("450px");

	txtOldPassword.focus();
	txtOldPassword.setWidth("200px");
	txtNewPassword.setWidth("200px");
	txtConfirmNewPassword.setWidth("200px");

	txtOldPassword.setMaxLength(200);
	txtNewPassword.setMaxLength(200);
	txtConfirmNewPassword.setMaxLength(200);

	btnSave.addStyleName("button-blue");
	btnCancel.addStyleName("button-red");
	btnSave.setWidth("70px");
	btnCancel.setWidth("70px");
	btnSave.setDisableOnClick(true);
	btnCancel.setDisableOnClick(true);

	row1Validate.setVisible(false);
	row2Validate.setVisible(false);
	row5Validate.setVisible(false);

	mainlayout.setSpacing(true);
	mainlayout.setSizeUndefined();

	changePasswordLayout.setSpacing(true);
	changePasswordLayout.setMargin(true);
	changePasswordLayout.setSizeUndefined();
	changePasswordLayout.addStyleName("change-password-panel");

	headerlayout.setSpacing(true);
	headerlayout.setSizeUndefined();

	VerticalLayout rootlayout = new VerticalLayout();
	rootlayout.setSizeFull();

	VerticalLayout formLayout = new VerticalLayout();
	formLayout.setId("formPanel");
	formLayout.setSizeFull();
	formLayout.setSpacing(true);

	HorizontalLayout row1 = new HorizontalLayout();
	HorizontalLayout row2 = new HorizontalLayout();
	HorizontalLayout row3 = new HorizontalLayout();
	HorizontalLayout row4 = new HorizontalLayout();
	HorizontalLayout row5 = new HorizontalLayout();
	HorizontalLayout row6 = new HorizontalLayout();
	HorizontalLayout row7 = new HorizontalLayout();

	row1.setSpacing(true);
	row2.setSpacing(true);
	row3.setSpacing(true);
	row4.setSpacing(true);
	row5.setSpacing(true);
	row6.setSpacing(true);
	row7.setSpacing(true);
	row1Validate.setSpacing(true);
	row2Validate.setSpacing(true);
	row5Validate.setSpacing(true);

	row1.setWidth("450px");
	row2.setWidth("450px");
	row3.setWidth("450px");
	row4.setWidth("450px");
	row5.setWidth("450px");
	row6.setWidth("450px");
	row1Validate.setWidth("450px");
	row2Validate.setWidth("450px");
	row5Validate.setWidth("450px");

	row1.addComponent(lblOldPassword);
	row1.addComponent(txtOldPassword);
	row1.setComponentAlignment(lblOldPassword, Alignment.MIDDLE_LEFT);
	row1.setComponentAlignment(txtOldPassword, Alignment.MIDDLE_LEFT);

	row1Validate.addComponent(lblSepValidate1);
	row1Validate.addComponent(this.lblOldPasswordValidateMessage);
	row1Validate.setComponentAlignment(this.lblOldPasswordValidateMessage, Alignment.TOP_LEFT);

	row2.addComponent(lblNewPassword);
	row2.addComponent(txtNewPassword);
	row2.setComponentAlignment(lblNewPassword, Alignment.MIDDLE_LEFT);
	row2.setComponentAlignment(txtNewPassword, Alignment.MIDDLE_LEFT);

	row2Validate.addComponent(lblSepValidate2);
	row2Validate.addComponent(this.lblNewPasswordValidateMessage);
	row2Validate.setComponentAlignment(this.lblNewPasswordValidateMessage, Alignment.TOP_LEFT);

	row3.addComponent(lblSep1);
	row3.addComponent(lblPasswordHint1);
	row3.setComponentAlignment(lblSep1, Alignment.MIDDLE_LEFT);
	row3.setComponentAlignment(lblPasswordHint1, Alignment.MIDDLE_LEFT);

	row4.addComponent(lblSep2);
	row4.addComponent(lblPasswordHint2);
	row4.setComponentAlignment(lblSep2, Alignment.MIDDLE_LEFT);
	row4.setComponentAlignment(lblPasswordHint2, Alignment.MIDDLE_LEFT);

	row5.addComponent(lblConfirmNewPassword);
	row5.addComponent(txtConfirmNewPassword);
	row5.setComponentAlignment(lblConfirmNewPassword, Alignment.MIDDLE_LEFT);
	row5.setComponentAlignment(txtConfirmNewPassword, Alignment.MIDDLE_LEFT);

	row5Validate.addComponent(lblSepValidate5);
	row5Validate.addComponent(this.lblConfirmNewPasswordValidateMessage);
	row5Validate.setComponentAlignment(this.lblConfirmNewPasswordValidateMessage, Alignment.TOP_LEFT);

	row7.addComponent(btnSave);
	// row6.addComponent(btnCancel);

	formLayout.addComponent(row1);
	formLayout.addComponent(row1Validate);
	formLayout.addComponent(row2);
	formLayout.addComponent(row2Validate);
	formLayout.addComponent(row3);
	formLayout.addComponent(row4);
	formLayout.addComponent(row5);
	formLayout.addComponent(row5Validate);
	formLayout.addComponent(row6);
	formLayout.addComponent(row7);

	formLayout.setComponentAlignment(row1, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row2, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row3, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row4, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row5, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row6, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row7, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row1Validate, Alignment.TOP_CENTER);
	formLayout.setComponentAlignment(row2Validate, Alignment.TOP_CENTER);
	formLayout.setComponentAlignment(row5Validate, Alignment.TOP_CENTER);

	headerlayout.addComponent(lblTitle);
	headerlayout.setComponentAlignment(lblTitle, Alignment.BOTTOM_CENTER);

	changePasswordLayout.addComponent(headerlayout);
	changePasswordLayout.addComponent(formLayout);
	changePasswordLayout.setComponentAlignment(formLayout, Alignment.MIDDLE_CENTER);

	mainlayout.addComponent(changePasswordLayout);
	mainlayout.addComponent(sepFooterlayout1);
	mainlayout.addComponent(sepFooterlayout2);
	mainlayout.addComponent(lblFooter);
	mainlayout.setComponentAlignment(changePasswordLayout, Alignment.MIDDLE_CENTER);
	mainlayout.setComponentAlignment(lblFooter, Alignment.MIDDLE_CENTER);

	rootlayout.addComponent(mainlayout);
	rootlayout.setComponentAlignment(mainlayout, Alignment.MIDDLE_CENTER);
	this.setContent(rootlayout);
    }

    @Override
    public void initListener() {
	changePasswordPanelSaveButtonClickListener.setBasedComponent(this);
	changePasswordPanelSaveButtonClickListener.setAutoEnabled(true);
	this.btnSave.addClickListener(changePasswordPanelSaveButtonClickListener);
    }

    @Override
    public void initValidator() {
	this.txtOldPassword.setRequired(true);
	this.txtNewPassword.setRequired(true);
	this.txtConfirmNewPassword.setRequired(true);
    }

    public void initData() {
	try {
	    PasswordPolicyBean passwordPolicyBean = passwordPolicyBAO.getPasswordPolicy();
	    if (passwordPolicyBean != null) {

		this.passwordPolicyBean = passwordPolicyBean;

		String[] params1 = new String[4];
		params1[0] = passwordPolicyBean.getMinPasswordLength() == null ? "" : passwordPolicyBean.getMinPasswordLength().toString();
		params1[1] = passwordPolicyBean.getMinUpperCase() == null ? "" : passwordPolicyBean.getMinUpperCase().toString();
		params1[2] = passwordPolicyBean.getMinNumeric() == null ? "" : passwordPolicyBean.getMinNumeric().toString();
		params1[3] = passwordPolicyBean.getMinSpecialCharacter() == null ? "" : passwordPolicyBean.getMinSpecialCharacter()
			.toString();

		String[] params2 = new String[1];
		params2[0] = passwordPolicyBean.getEnforePasswordHistory() == null ? "" : passwordPolicyBean.getEnforePasswordHistory()
			.toString();

		lblPasswordHint1.setValue(this.messageBAO.getMessageWithParams("com.fw.ui.view.common.ChangePasswordPanel.label.hint1",
			params1));

		lblPasswordHint2.setValue(this.messageBAO.getMessageWithParams("com.fw.ui.view.common.ChangePasswordPanel.label.hint2",
			params2));
	    } else {
		String msg = this.messageBAO.getMessage("message.error.common");
		Notification notification = new Notification("Warning", msg, Notification.Type.WARNING_MESSAGE, true);
		notification.setDelayMsec(1000);
		notification.show(Page.getCurrent());
	    }
	} catch (Exception e) {
	    String msg = this.messageBAO.getMessage("message.error.common");
	    Notification notification = new Notification("Warning", msg, Notification.Type.WARNING_MESSAGE, true);
	    notification.setDelayMsec(1000);
	    notification.show(Page.getCurrent());
	}
    }

    public VerticalLayout getChangePasswordLayout() {
	return changePasswordLayout;
    }

    public void removeBoxStyle() {
	changePasswordLayout.removeStyleName("change-password-panel");
    }

    public void removeHeaderLayout() {
	changePasswordLayout.removeComponent(headerlayout);
    }

    public void removeFooterLayout() {
	mainlayout.removeComponent(sepFooterlayout1);
	mainlayout.removeComponent(sepFooterlayout2);
	mainlayout.removeComponent(lblFooter);
    }

    public PasswordPolicyBean getPasswordPolicyBean() {
	return passwordPolicyBean;
    }

    public void setPasswordPolicyBean(PasswordPolicyBean passwordPolicyBean) {
	this.passwordPolicyBean = passwordPolicyBean;
    }

    public Label getLblOldPasswordValidateMessage() {
	return lblOldPasswordValidateMessage;
    }

    public Label getLblNewPasswordValidateMessage() {
	return lblNewPasswordValidateMessage;
    }

    public Label getLblConfirmNewPasswordValidateMessage() {
	return lblConfirmNewPasswordValidateMessage;
    }

    public PasswordField getTxtOldPassword() {
	return txtOldPassword;
    }

    public PasswordField getTxtNewPassword() {
	return txtNewPassword;
    }

    public PasswordField getTxtConfirmNewPassword() {
	return txtConfirmNewPassword;
    }

    public String getOldPassword() {
	return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
	this.oldPassword = oldPassword;
    }

    public HorizontalLayout getRow1Validate() {
	return row1Validate;
    }

    public HorizontalLayout getRow2Validate() {
	return row2Validate;
    }

    public HorizontalLayout getRow5Validate() {
	return row5Validate;
    }

    public Long getUserId() {
	return userId;
    }

    public void setUserId(Long userId) {
	this.userId = userId;
    }

    public String getNavigatePath() {
	return navigatePath;
    }

    public void setNavigatePath(String navigatePath) {
	this.navigatePath = navigatePath;
    }

    public boolean isRequireNewLogin() {
	return requireNewLogin;
    }

    public void setRequireNewLogin(boolean requireNewLogin) {
	this.requireNewLogin = requireNewLogin;
    }

}
